package com.sz.szaiagent.demo.invoke;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.ContentType;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONUtil;

/**
 * 使用http 调用ai
 */
public class HttpAiInvoke {
    public static void main(String[] args) {
        String url = "https://dashscope.aliyuncs.com/compatible-mode/v1/chat/completions";
        String apiKey = TestApiKey.API_KEY; // 替换为你的真实 API Key

        JSONArray messages = new JSONArray();
        messages.put(JSONUtil.createObj()
                .set("role", "system")
                .set("content", "You are FileBasedChatMemory helpful assistant."));
        messages.put(JSONUtil.createObj()
                .set("role", "user")
                .set("content", "你是谁？"));

        String body = JSONUtil.createObj()
                .set("model", "qwen-plus")
                .set("messages", messages)
                .toString();

        try {
            HttpResponse response = HttpRequest.post(url)
                    .header("Authorization", "Bearer " + apiKey)
                    .header("Content-Type", ContentType.JSON.getValue())
                    .body(body)
                    .execute();

            if (response.getStatus() == 200) {
                System.out.println(response.body());
            } else {
                System.err.println("请求失败，状态码: " + response.getStatus());
                System.err.println("响应内容: " + response.body());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}